﻿using Architecture.Domain;
using Architecture.Domain.Dto;
using Architecture.Domain.Entity;
using Architecture.Domain.RBAC;
using Architecture.Repository.Impl;
using Architecture.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Service
{
    public class MenuService : IMenuService
    {
        protected readonly IMenuRepository menuRepository;
        protected readonly IRoleRepository roleRepository;
        protected readonly IPrivilegRepository privilegRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="menuRepository"></param>
        public MenuService(IMenuRepository menuRepository, IRoleRepository roleRepository, IPrivilegRepository privilegRepository)
        {
            this.menuRepository = menuRepository;
            this.roleRepository = roleRepository;
            this.privilegRepository = privilegRepository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(Menu model)
        {
            model.AddName = "张三";
            model.AddTime = DateTime.Now;
            return await menuRepository.AddAsync(model);
        }
        /// <summary>
        /// 递归删除子集
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> MenuDelete (int model)
        {
            var menulist=await menuRepository.GetAsync(model);
            if (menulist == null)
            {
                return 0;
            }
            await GetMenus(menulist);
            return 1;
            #region  废料
            //  var query = (await menuRepository.GetAllAsync()).Where(x => x.MenuId == model.MenuId).FirstOrDefault();
            //var gh=  menuRepository.isDelte(query.MenuId);

            //  if(gh!=null)
            //  {
            //      return 1;
            //  }
            //  else
            //  {
            //      return -1;
            //  }
            //var res = (await menuRepository.GetAllAsync()).Any(x => x.PId == model.MenuId);
            //if (res)
            //{
            //    menuRepository.DeleteAsync(model);
            //    var list = (await menuRepository.GetAllAsync()).Where(x => x.PId == model.MenuId).ToList();
            //    foreach (var item in list)
            //    {
            //        await MenuDelete(item);
            //    }
            //}
            //else
            //{
            //    var ress = await menuRepository.GetAsync(model.MenuId);
            //    menuRepository.DeleteAsync(ress);
            //}
            //return 1;
            #endregion
        }
        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task  GetMenus(Menu t)
        {
            await menuRepository.isDelte(t.MenuId);
            var list = (await menuRepository.GetAllAsync()).Where(x=>x.PId == t.MenuId).ToList();
            foreach (var item in list)
            {
                await GetMenus(item);
            }
        }
        //菜单列表
        public async Task<PageListDto<MenuDto>> GetAllAsync(int pageIndex, int pageSize, string? menuName)
        {
            PageListDto<MenuDto> page = new PageListDto<MenuDto>();
            var menu= await menuRepository.GetAllAsync();
            var list = (from m in await menuRepository.GetAllAsync() select new MenuDto{ 
                MenuId = m.MenuId,
                MenuName = m.MenuName,
                Url = m.Url,
                PId = m.PId,
                AddName = m.AddName,
                AddTime = m.AddTime,
                UpdateName = m.UpdateName,
                UpdateTime = m.UpdateTime,
                PName=(from p in menu where p.MenuId==m.PId select new Menu
                {
                    MenuName=p.MenuName,
                }).FirstOrDefault()
            }).ToList();
            if (menuName != null)
            {
                list=list.Where(x=>x.MenuName.Contains(menuName)).ToList();
            }
            page.ListCount = list.Count;
            page.PageCount = (int)Math.Ceiling((page.ListCount * 1.0) / pageSize);
            page.Lists =list.OrderByDescending(x=>x.MenuId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return page;
        }
        //菜单查看详情
        public async Task<Menu> GetByIdAsync(int id)
        {
            return await menuRepository.GetAsync(id);
        }
        //父级菜单下拉框
        public async Task<List<Menu>> GetNameAsync()
        {
            var list= await menuRepository.GetAllAsync();
            return list;
        }
        //菜单修改
        public async Task<int> UpdateAsync(Menu model)
        {
            var list = await menuRepository.GetAsync(model.MenuId);
            list.MenuName = model.MenuName;
            list.Url = model.Url;
            list.PId = model.PId;
            list.UpdateName = "李四";
            list.UpdateTime = DateTime.Now;
            return await menuRepository.UpdateAsync(list);
        }
        //树形菜单
        public async Task<List<Cascader>> MenuList(int id=0)
        {
            var menulist=await menuRepository.GetAllAsync();
            var menu = menulist.Where(x=>x.PId==id).ToList();
            List<Cascader> list = new List<Cascader>();
            foreach (var item in menu)
            {
                Cascader model= new Cascader();
                model.Value = item.MenuId;
                model.Label= item.MenuName;
                model.Arress= item.Url;
                model.Children = await MenuList(item.MenuId);
                list.Add(model);
            }
            return list;
        }
        /// <summary>
        /// 树形菜单反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<object> PrivileFan(int id)
        {
            var Privile = await privilegRepository.GetAllAsync();
            var list= Privile.Where(x=>x.Rid==id).ToList();
            return list;
        }
        /// <summary>
        /// 角色菜单关联添加
        /// </summary>
        /// <param name="rId"></param>
        /// <param name="mId"></param>
        /// <returns></returns>
        public async Task<int> GetRelatedAdd(int rId, List<int> mId)
        {
            foreach (var item in mId)
            {
                var related = new Privileges()
                {
                    Rid = rId,
                    MId = item
                };
                await privilegRepository.AddAsync(related);
            }
            return 1;
        }
    }
}
